package saucaca.Adapter;

import java.util.ArrayList;
import java.util.List;

import saucaca.Object.SongObject;
import saucaca.Util.AppController;
import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.android.volley.toolbox.ImageLoader;
import com.lemon.kenhnhacvn.BaseActivity;
import com.lemon.kenhnhacvn.BaseEngine;
import com.lemon.kenhnhacvn.R;

public class CustomAdapterVideoListview extends ArrayAdapter<SongObject> {
	Context context;
	List<SongObject> data = new ArrayList<SongObject>();
	ImageLoader imageLoader;
	boolean isBXH = true;
	boolean isSendlist=false;

	public CustomAdapterVideoListview(Context context, List<SongObject> data, boolean isBXH) {
		super(context, R.layout.item_bxh_listview, data);
		this.context = context;
		this.data = data;
		this.isBXH = isBXH;
	}
	public CustomAdapterVideoListview(Context context, List<SongObject> data, boolean isBXH,boolean isSendlist) {
		super(context, R.layout.item_bxh_listview, data);
		this.context = context;
		this.data = data;
		this.isBXH = false;
		this.isSendlist=isSendlist;
	}
	
	public void update(List<SongObject> data){
		this.data=data;
		notifyDataSetChanged();
	}
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		View row = convertView;
		RecordHolder holder = null;
		if (row == null) {
			LayoutInflater inflater = ((Activity) context).getLayoutInflater();
			row = inflater.inflate(R.layout.item_bxh_listview, parent, false);
			holder = new RecordHolder();
			try {
				imageLoader=((BaseActivity)context).imageLoader;
			} catch (Exception e) {
				imageLoader=AppController.getInstance().getImageLoader();
			}
			holder.relati_Rank = (RelativeLayout) row.findViewById(R.id.item_bxh_relativeRank);
			holder.relati_Yeuthich = (RelativeLayout) row.findViewById(R.id.item_bxh_retaliYeuthich);
			holder.txtTitle = (TextView) row.findViewById(R.id.item_bxh_title);
			holder.txtArtist = (TextView) row.findViewById(R.id.item_bxh_artist);
			holder.imageItem = (ImageView) row.findViewById(R.id.item_bxh_imgAvatar);
			holder.txtLuotnghe = (TextView) row.findViewById(R.id.item_bxh_textluotnghe);
			holder.txtYeuthich = (TextView) row.findViewById(R.id.item_bxh_textYeuthich);
			holder.txtRank = (TextView) row.findViewById(R.id.item_bxh_textViewRank);
			holder.imgVideoicon = (ImageView) row.findViewById(R.id.item_bxh_Videoicon);
			holder.imgRank = (ImageView) row.findViewById(R.id.item_bxh_imgRankColor);
			row.setTag(holder);
		} else {
			holder = (RecordHolder) row.getTag();
		}
		SongObject item = data.get(position);
		imageLoader.get(item.getAVATAR(), ImageLoader.getImageListener(holder.imageItem,
			         R.drawable.ic_default, R.drawable.ic_default));
		
		if (isBXH == false) {
			holder.relati_Rank.setVisibility(View.INVISIBLE);
			holder.relati_Yeuthich.setVisibility(View.INVISIBLE);
			if(isSendlist){
				BaseEngine.setVideoclick(context, row, position,(ArrayList<SongObject>)data);
			}else
				BaseEngine.setVideoclick(context, row, item);
		}else{
			BaseEngine.setVideoclick(context, row, position,(ArrayList<SongObject>)data);
		}
		holder.txtTitle.setText(item.getTXNAME());
		holder.txtArtist.setText(item.getNAME_SINGERS());
		holder.txtRank.setText(Integer.toString((position + 1)));
		holder.txtLuotnghe.setText(BaseEngine.convertStringLongtoInteger(item.getISVIEW()));
		holder.txtYeuthich.setText("0");
		switch (position) {
		case 0:
			holder.imgRank.setImageResource(R.drawable.bxh_icon1);
			break;
		case 1:
			holder.imgRank.setImageResource(R.drawable.bxh_icon2);
			break;
		case 2:
			holder.imgRank.setImageResource(R.drawable.bxh_icon3);
			break;
		default:
			holder.imgRank.setImageResource(R.drawable.bxh_icon4);
			break;
		}
		return row;
	}

	static class RecordHolder {
		RelativeLayout relati_Rank;
		RelativeLayout relati_Yeuthich;
		ImageView imgRank;
		ImageView imgVideoicon;
		ImageView imageItem;
		TextView txtLuotnghe;
		TextView txtYeuthich;
		TextView txtRank;
		TextView txtTitle;
		TextView txtArtist;
	}
}
